#!/usr/bin/python
# -*- coding: utf-8 -*-
import math
import numpy as np
from sys import stdout
import os


"""
*****
*****
**O**
***X*
*****
"""
SPEC_NUM = 5
ROW_NUM = SPEC_NUM
COL_NUM = SPEC_NUM

ACTIONS = ['left', 'right']     # 探索者的可用动作

def print_state(S):
    print("\033[%d;%df" % (18, 0), end="")
    for i in range(ROW_NUM * COL_NUM):
        s = (S // math.pow(3, i)) % 3
        s = " " if (s == 0) else "X" if (s == 1) else "O"
        e = '' if ((i + 1) % ROW_NUM != 0) else '\n'
        print(f" {s}", end=e)
    return

def arr_to_state(arr):
    S = 0
    for i in range(COL_NUM):
        for j in range(ROW_NUM):
            S += arr[i, j] * math.pow(3, (ROW_NUM * i + j))
    return int(S)

if __name__ == "__main__":
    print("Q-learning")
    arr = np.array([
        [0, 0, 0, 0, 0],
        [0, 1, 2, 2, 0],
        [0, 0, 1, 2, 0],
        [0, 0, 2, 1, 0],
        [0, 0, 0, 0, 1],
    ])

    print(arr)

    S = arr_to_state(arr)
    print(f"S={S}")
    print_state(S, 0, 0)
    # os.system('clear')
    # print("\033c")
    print("\033[%d;%df" % (18, 0), end="")
    print(f"S={S}")
    print_state(S)